// body-data.js
var app = getApp()
var util = require('../../utils/util.js')
var iFDU = require('../../utils/iFDU.js');
Page({

  data: {
    config: app.config,
    globalData: app.globalData,
    currentIndex: 3,
    id: 0,
    bodyData:{
      0: 0,
      1: 0,
      2: 0,
      3: 0,
      4: 0,
      5: 0,
      6: 0
    },
    fixBodyDataName: [
      { k: 0, v: '胸围' },
      { k: 1, v: '腹围' },
      { k: 2, v: '袖长' },
      { k: 3, v: '身高' },
      { k: 4, v: '体重' },
      { k: 5, v: '领围' },
      { k: 6, v: '肩宽' },
    ],
    bodyDataName: [
      { k: 0, v: '胸围' },
      { k: 1, v: '腹围' },
      { k: 2, v: '袖长' },
      { k: 3, v: '身高' },
      { k: 4, v: '体重' },
      { k: 5, v: '领围' },
      { k: 6, v: '肩宽' },
    ],
    images: [
      'chest',
      'abdomen',
      'sleeve',
      'height',
      'weight',
      'collar',
      'shoulder',
    ],
    valueRanges: [
      util.convertRangeArray(70, 130),
      util.convertRangeArray(55, 120),
      util.convertRangeArray(52, 66),
      util.convertRangeArray(195, 150),
      util.convertRangeArray(99, 45),
      [33, 33.5, 34, 34.5, 35, 35.5, 36, 36.5, 37, 37.5, 38, 38.5, 39, 39.5, 40, 40.5, 41, 41.5, 42, 42.5, 43, 43.5, 44, 44.5, 45, 45.5, 46, 46.5, 47, 47.5, 48],
      util.convertRangeArray(35, 55)
    ],
    swiper: {
      indicatorDots: false,
      autoplay: false,
      interval: 2000,
      duration: 500,
    },
    wholeBodyPhoto: '',
    remark: '',
    needCb: 0,
  },

  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
        bodyData: JSON.parse(options.bodyShape),
        wholeBodyPhoto: decodeURIComponent(options.wholeBodyPhoto),
        remark: decodeURIComponent(options.remark),
      })
    }
    if (options.needCb) {
      this.setData({
        needCb: options.needCb
      });
    }
  },

  reSort: function (currentIndex) {
    var that = this;
    var fixBodyDataName = that.data.fixBodyDataName;
    var length = fixBodyDataName.length;
    if (currentIndex > length - 1) {
      return fixBodyDataName;
    }
    var offset = parseInt(length / 2);

    var ret = [];
    for (var i = currentIndex + length - offset; i < currentIndex + length + (length - offset); i++) {
      ret.push(fixBodyDataName[i % length]);
    }
    return ret;
  },

  changeBodyItem: function (e) {
    var currentIndex = e.detail.current;
    this.setData({
      currentIndex: currentIndex,
      bodyDataName: this.reSort(currentIndex)
    })
  },

  selectCurrent: function (e) {
    var idx = e.currentTarget.dataset.idx;
    this.setData({
      currentIndex: idx,
      bodyDataName: this.reSort(idx)
    })
  },

  pickerChange: function (e) {
    var idx = parseInt(e.currentTarget.dataset.idx);
    var bodyData = this.data.bodyData;
    bodyData[idx] = this.data.valueRanges[idx][e.detail.value];
    this.setData({
      bodyData: bodyData
    })

    this.next();
  },

  previous: function () {
    var that = this;
    var currentIndex = this.data.currentIndex;
    if (currentIndex > 0) {
      this.setData({
        currentIndex: currentIndex - 1,
        bodyDataName: this.reSort(currentIndex - 1)
      });
    } else {
      this.setData({
        currentIndex: that.data.images.length - 1,
        bodyDataName: this.reSort(that.data.images.length - 1)
      });
    }
  },

  next: function () {
    var that = this;
    var currentIndex = this.data.currentIndex;
    if (currentIndex < that.data.images.length - 1) {
      this.setData({
        currentIndex: currentIndex + 1,
        bodyDataName: this.reSort(currentIndex + 1)
      });
    } else {
      this.setData({
        currentIndex: 0,
        bodyDataName: this.reSort(0)
      });
    }
  },

  uploadWholeBodyPhoto: function (e) {
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: that.data.config.baseApiUrl + 'bodyshape/uploadWholBodyPhoto',
          filePath: tempFilePaths[0],
          name: 'photo',
          formData: {
            userId: that.data.globalData.userId,
            channelId: '21'
            
          },
          success: function (res) {
            var data = JSON.parse(res.data);
            that.setData({
              wholeBodyPhoto: data.data.whole_body_photo
            })
          }
        })
      },
    })
  },

  editRemark: function (e) {
    this.setData({
      remark: e.detail.value
    });
  },

  saveBodyData: function (e) {
    var that = this;
    if (that.data.bodyData[3] <= 0) {
      iFDU.toast.showToast({
        title: '身高为必填项',
        mask: false
      })
      return;
    }
    if (that.data.bodyData[4] <= 0) {
      iFDU.toast.showToast({
        title: '体重为必填项',
        mask: false
      })
      return;
    }
    // if (!that.data.remark || that.data.remark == '') {
    //   iFDU.toast.showToast({
    //     title: '请输入身型数据标签',
    //     mask: false
    //   })
    //   return;
    // }
    if (that.data.id && that.data.id != 0) {

      iFDU.api.doGet(
        'bodyshape/updateBodyShape',
        {
          userId: that.data.globalData.userId,
          id: that.data.id,
          rnd: Math.random(),
          bustLength: that.data.bodyData[0],
          waistline: that.data.bodyData[1],
          sleeveLength: that.data.bodyData[2],
          height: that.data.bodyData[3],
          weight: that.data.bodyData[4],
          collarLength: that.data.bodyData[5],
          shouderLength: that.data.bodyData[6],
          remark: that.data.remark,
          wholeBodyPhoto: that.data.wholeBodyPhoto
        },
        function (data) {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2];
          wx.navigateBack({

          });
        }
      );
    } else {
      iFDU.api.doGet(
        'bodyshape/addBodyShape',
        {
          userId: that.data.globalData.userId,
          rnd: Math.random(),
          bustLength: that.data.bodyData[0],
          waistline: that.data.bodyData[1],
          sleeveLength: that.data.bodyData[2],
          height: that.data.bodyData[3],
          weight: that.data.bodyData[4],
          collarLength: that.data.bodyData[5],
          shouderLength: that.data.bodyData[6],
          remark: that.data.remark,
          wholeBodyPhoto: that.data.wholeBodyPhoto
        },
        function (data) {
          if (!that.data.needCb || that.data.needCb == 0) {
            wx.navigateBack({
            });
          } else {
            iFDU.api.doGet(
              'bodyshape/getBodyShapeById',
              {
                userId: that.data.globalData.userId,
                id: data.id
              },
              function (data1) {
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setData({
                  bodyShapeId: data.id,
                  bodyShape: data1.bodyShape
                })
                wx.navigateBack({
                });
              }
            )
          }
        }
      );
    }
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '你的衬衫，应该是定制的！',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})